package cn.ren.cloud.controller;

import cn.ren.cloud.dto.CourseDto;
import cn.ren.cloud.service.ICourseService;
import cn.ren.cloud.domain.Course;
import cn.ren.cloud.query.CourseQuery;
import cn.ren.cloud.result.JsonResult;
import cn.ren.cloud.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author 23508
 */
@RestController
@RequestMapping("/course")
public class CourseController {

    public final ICourseService courseService;

    public CourseController(ICourseService courseService) {
        this.courseService = courseService;
    }

    /**
     * 保存和修改公用的
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JsonResult save(@RequestBody @Valid CourseDto courseDto) {
        courseService.saveCourseAll(courseDto);
        return JsonResult.success();
    }

    /**
     * 删除对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id) {
        courseService.deleteById(id);
        return JsonResult.success();
    }

    /**
     * 获取对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public JsonResult get(@PathVariable("id") Long id) {
        return JsonResult.success(courseService.selectById(id));
    }


    /**
     * 查询所有对象
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JsonResult list() {
        return JsonResult.success(courseService.selectList(null));
    }


    /**
     * 带条件分页查询数据
     */
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public JsonResult page(@RequestBody CourseQuery query) {
        Page<Course> page = new Page<Course>(query.getPage(), query.getRows());
        page = courseService.selectPage(page);
        return JsonResult.success(new PageList<Course>(page.getTotal(), page.getRecords()));
    }

    @RequestMapping(value = "/onLineCourse/{courseId}", method = RequestMethod.POST)
    public JsonResult onLineCourse(@PathVariable("courseId") Long courseId) {
        courseService.onLineCourse(courseId);
        return JsonResult.success();
    }

    /**
     * 获取课程详情
     *
     * @param id 课程ID
     * @return 课程ID详情
     */
    @RequestMapping(value = "/detail/data/{courseId}", method = RequestMethod.GET)
    public JsonResult detailDate(@PathVariable("courseId") Long id) {

        return JsonResult.success(courseService.detailDate(id));
    }

    /**
     * 通过IDS获取详情
     *
     * @param courseIds 课程IDS
     * @return 课程详情
     */
    @RequestMapping(value = "/info/{courseIds}", method = RequestMethod.GET)
    public JsonResult getInfo(@PathVariable("courseIds") String courseIds) {
        return JsonResult.success(courseService.getInfo(courseIds));
    }

}
